package com.qf.day61.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.day61.entity.BasicInfo;
import com.qf.day61.entity.Brand;
import com.qf.day61.entity.Category;
import com.qf.day61.entity.Goods;
import com.qf.day61.service.BasicInfoService;
import com.qf.day61.service.BrandService;
import com.qf.day61.service.CategoryService;
import com.qf.day61.service.GoodsService;
import com.qf.day61.utils.CommonConstants;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Arrays;
import java.util.List;

@Controller
@RequestMapping("/goods")
public class GoodsController {
    private final CategoryService categoryService;
    private final BrandService brandService;
    private final GoodsService goodsService;
    private final BasicInfoService basicInfoService;

    public GoodsController(CategoryService categoryService,
                           BrandService brandService,
                           GoodsService goodsService,
                           BasicInfoService basicInfoService) {
        this.categoryService = categoryService;
        this.brandService = brandService;
        this.goodsService = goodsService;
        this.basicInfoService = basicInfoService;
    }

    @RequestMapping("/list")
    public String list(Model model, @RequestParam(value = "categoryId", defaultValue = "1")
            Integer categoryId, Integer brandId,
                       String goodsName,
                       @RequestParam(value = "page", defaultValue = "1") Integer page){
        final List<Category> categoryList = categoryService.findAll();
        final List<Brand> brandList = brandService.findAllByCategoryId(categoryId);
        final Category category = categoryService.findById(categoryId);
        PageHelper.startPage(page, CommonConstants.PAGE_SIZE);
        final List<Goods> goodsList = goodsService.findAllByBrandIdAndCategoryIdAndGoodsName(brandId, categoryId, goodsName);
        PageInfo info = new PageInfo(goodsList);
        model.addAttribute("bean", info);
        model.addAttribute("category", category);
        model.addAttribute("categoryList", categoryList);
        model.addAttribute("brandList", brandList);
        model.addAttribute("brandId", brandId);
        model.addAttribute("goodsName", goodsName);
        return "/home";
    }

    // 路径跳转时需要使用/detail/3模式的路径，此时传递的变量值为3
    @RequestMapping("/detail/{id}")
    public String detail(@PathVariable("id") Integer id, Model model){
        final Goods goods = goodsService.findById(id);
        model.addAttribute("goods", goods);
        List<BasicInfo> step1 = basicInfoService.findStep(id, 1);
        List<BasicInfo> step2 = basicInfoService.findStep(id, 2);
        List<BasicInfo> step3 = basicInfoService.findStep(id, 3);
        model.addAttribute("step1", step1);
        model.addAttribute("step2", step2);
        model.addAttribute("step3", step3);
        return "/info";
    }

    @PostMapping("/price")
    public String price(Integer[]property1, Integer[]property2, Integer[]property3){
        System.out.println(Arrays.toString(property1));
        System.out.println(Arrays.toString(property2));
        System.out.println(Arrays.toString(property3));
        return "/price";
    }
}